from PIL import Image, ImageFont, ImageDraw, ImageFilter
from random import random, randint, choice


def get_random_color():
    return (randint(0, 255), randint(0, 255), randint(0, 255))


def generate_image(length, lines):
    s = 'q1w2e3r4t5y6u7VTGBYHNMJUKIOi8o9a0s9d8f7g6h5j4k3l2z1x2c3vbnmQAZWSXEDRF'
    size = (90, 40)
    yzm = Image.new('RGB', size, color=get_random_color())
    font = ImageFont.truetype('../fonts/consolai.ttf', size=40)
    draw = ImageDraw.Draw(yzm)
    code = ''
    for i in range(length):
        char = choice(s)
        code += char
        draw.text(
            xy=(5 + randint(4, 7) + 20 * i, randint(2, 6)),
            text=char,
            fill=get_random_color(),
            font=font,
            align='center',

        )

    for i in range(lines):
        x1 = randint(0, 130)
        y1 = randint(0, 25)
        x2 = randint(0, 130)
        y2 = randint(25, 50)
        draw.line(
            xy=((x1, y1), (x2, y2)),
            fill=get_random_color()
        )
    yzm.filter(ImageFilter.EDGE_ENHANCE)
    # yzm.filter(ImageFilter)
    return yzm, code
